﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DAL;
namespace BLL.Admin
{
    // role : 0 -> admin, 1-> manager
    // khoa : false , mo : true
    public class UserDao
    {
        public int UserId { get; set; }
        public string UserName { get; set; }
        public string Email { get; set; }
        public string DienThoai { get; set; }
        public string Role { get; set; }
        public bool Khoa { get; set; }
        public bool Mo { get; set; }
        public string Staus { get; set; }
    }
    public class UserHelper
    {
        public static void Add_User(string UserName,string Pass,string Email,string DienThoai,bool Status,int Role)
        {
            var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");
            UserAdmin user = new UserAdmin();
            user.UserName = UserName;
            user.Password = Tool.MaHoa(Pass);
            user.Email = Email;
            user.DienThoai = DienThoai;
            user.CreateDate = DateTime.Now;
            user.DaKichHoat = Status;
            user.Role = Role;
            db.UserAdmins.InsertOnSubmit(user);
            db.SubmitChanges();
        }
        public static void Update_User(int UserId,string UserName, string Pass, string Email, string DienThoai, int Role)
        {
            var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");
            UserAdmin user = db.UserAdmins.FirstOrDefault(k=>k.UserId==UserId);
            user.UserName = UserName;
            if (Pass != null)
            {
                user.Password = Tool.MaHoa(Pass);
            }                        
            user.Email = Email;            
            user.DienThoai = DienThoai;                   
            user.Role = Role;            
            db.SubmitChanges();
        }
        public static UserAdmin Get_UserId(int UserId)
        {
            var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");
            UserAdmin user = db.UserAdmins.FirstOrDefault(k => k.UserId == UserId);
            return user;
        }
        public static UserAdmin Get_User_Email(string Email)
        {
            var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");
            UserAdmin user = db.UserAdmins.FirstOrDefault(k => k.Email == Email);
            return user;
        }
        public static void KhoaMoTaiKhoan(int UserId, bool Status)
        {
            var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");
            UserAdmin user = db.UserAdmins.FirstOrDefault(k => k.UserId == UserId);
            user.DaKichHoat = Status;
            db.SubmitChanges();
        }
        public static List<UserDao> List_User_Search(string UserName,bool Status)
        {
            var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");            
            var query = from user in db.UserAdmins
                        where user.UserName.Contains(UserName) && (user.DaKichHoat== Status)
                        orderby user.UserName ascending
                        select new UserDao
                        {
                            UserId=user.UserId,
                            UserName=user.UserName,
                            Email=user.Email,
                            DienThoai=user.DienThoai,
                            Role = user.Role==0?"Administrator":"Manager",
                            Khoa= user.DaKichHoat == true,
                            Mo= user.DaKichHoat==false,
                            Staus = user.DaKichHoat==true?"Mở":"Khóa",                            
                        };
            return query.ToList();
        }
        public static bool login(string Email, string MatKhau)
        {
            var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");            
            UserAdmin user = db.UserAdmins.SingleOrDefault(k=>k.Email==Email && k.Password==Tool.MaHoa(MatKhau) && k.DaKichHoat==true);
            if (user != null)
            {
                return true;
            }
            else return false;
        }
        public static bool KiemTraEmailTonTai(string Email)
        {
            var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");
            UserAdmin user = db.UserAdmins.FirstOrDefault(k => k.Email == Email);
            if (user != null)
            {
                return true;
            }
            else return false;
        }
    }
}